package com.yiboshi.rehrs.util.xsd;

import cn.hutool.core.codec.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.util.Random;

public class AESUtilNewYz {
    public static final String gongYao = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCZMYXvNpLgq4PX+LlWem7lzScgKGNRVJYGbRVEKO+Kaj5BdUpqucNo5+4rM6uiYimV6XSEV9eH8cHbl2ea4fN7rm/TFa6nSTdVTYDZt0WSxudfQ52QgZvl6cW7EFQCB589y52cFMAaL1wz9aJu3Uqz2+nVk/KZrSof65l6zPq1DwIDAQAB";

    static final String CHAR_TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    public static String generateRandomString(int i) {
        Random random = new Random();
        StringBuilder sb = new StringBuilder(i);
        for (int i2 = 0; i2 < i; i2++) {
            sb.append(CHAR_TABLE.charAt(random.nextInt(62)));
        }
        return sb.toString();
    }

    private static final String AES = "AES";
    private static final String AES_CBC_PKCS7 = "AES/CBC/PKCS5Padding";
    private static final int IV_SIZE = 16;
    private static final int KEY_SIZE = 24;
    public static final String STRING_CHARSET_NAME = "UTF-8";
    public static final Charset CHARSET = Charset.forName(STRING_CHARSET_NAME);


    public static String encrypt(String str, String str2) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(str2.getBytes(STRING_CHARSET_NAME), AES);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
        Cipher cipher = Cipher.getInstance(AES_CBC_PKCS7);
        cipher.init(1, secretKeySpec, ivParameterSpec);
        byte[] doFinal = cipher.doFinal(str.getBytes());
        /*if (Build.VERSION.SDK_INT >= 26) {
            return Base64.getEncoder().encodeToString(doFinal);
        }
        return Base64Utils.encode(doFinal);*/
        return Base64.encode(doFinal);
    }

    public static String decrypt(String str, String str2) throws Exception {
        byte[]  decode = Base64.decode(str);
        SecretKeySpec secretKeySpec = new SecretKeySpec(str2.getBytes(STRING_CHARSET_NAME), AES);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
        Cipher cipher = Cipher.getInstance(AES_CBC_PKCS7);
        cipher.init(2, secretKeySpec, ivParameterSpec);
        return new String(cipher.doFinal(decode));
    }



}
